package com.webside.cdn.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

public class UploadFile {
	private static final String TAG = "uploadFile";  
    private static final int TIME_OUT = 10 * 10000000; // ��ʱʱ��  
    private static final String CHARSET = "utf-8"; // ���ñ���  
    public static final String SUCCESS = "1";  
    public static final String FAILURE = "0";  
  
    public static String uploadFile(InputStream is, String fileName) {  
        String BOUNDARY = UUID.randomUUID().toString(); // �߽���?������  
        String PREFIX = "--", LINE_END = "\r\n";  
        String CONTENT_TYPE = "multipart/form-data"; // ��������  
        String RequestURL = "http://192.168.1.105:15020/uploadfile";  
        try {  
            URL url = new URL(RequestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setReadTimeout(TIME_OUT);  
            conn.setConnectTimeout(TIME_OUT);  
            conn.setDoInput(true); // ����������  
            conn.setDoOutput(true); // ���������? 
            conn.setUseCaches(false); // ������ʹ�û���  
            conn.setRequestMethod("POST"); // ����ʽ  
            conn.setRequestProperty("Charset", CHARSET); // ���ñ���  
            conn.setRequestProperty("connection", "keep-alive");  
            conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="  
                    + BOUNDARY);  
            if (is != null) {
                OutputStream outputSteam = conn.getOutputStream();  
  
                DataOutputStream dataOut = new DataOutputStream(outputSteam);  
                StringBuffer sb = new StringBuffer();  
                sb.append(PREFIX);  
                sb.append(BOUNDARY);
                sb.append(LINE_END);
  
                sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""  
                        + fileName + "\"" + LINE_END);  
                sb.append("Content-Type: application/octet-stream; charset="  
                        + CHARSET + LINE_END);  
                sb.append(LINE_END);  
                dataOut.write(sb.toString().getBytes()); 
                byte[] bytes = new byte[1024];  
                int len = 0;  
                while ((len = is.read(bytes)) != -1) {  
                	dataOut.write(bytes, 0, len);  
                }  
                is.close();
                dataOut.write(LINE_END.getBytes());  
                byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)  
                        .getBytes();  
                dataOut.write(end_data);  
                dataOut.flush();
                
                int res = conn.getResponseCode();
                if (res == 200) {
                	sb = new StringBuffer(); 
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
                    String line = null; 
                    while ((line = reader.readLine()) != null) { 
                        sb.append(line).append("\n");
                    } 
                    System.out.println(sb.toString());
                    return SUCCESS;
                }
                
            }  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return FAILURE;  
    }
    
    public static String uploadFile(File file) {  
        String BOUNDARY = UUID.randomUUID().toString(); // �߽���?������  
        String PREFIX = "--", LINE_END = "\r\n";  
        String CONTENT_TYPE = "multipart/form-data"; // ��������  
        String RequestURL = "http://192.168.1.111:15020/uploadfile";  
        try {  
            URL url = new URL(RequestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setReadTimeout(TIME_OUT);  
            conn.setConnectTimeout(TIME_OUT);  
            conn.setDoInput(true); 
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Charset", CHARSET);
            conn.setRequestProperty("connection", "keep-alive");  
            conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="  
                    + BOUNDARY);  
            if (file != null) {
                OutputStream outputSteam = conn.getOutputStream();  
  
                DataOutputStream dos = new DataOutputStream(outputSteam);  
                StringBuffer sb = new StringBuffer();  
                sb.append(PREFIX);  
                sb.append(BOUNDARY);  
                sb.append(LINE_END);
                
                sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""  
                        + file.getName() + "\"" + LINE_END);  
                sb.append("Content-Type: application/octet-stream; charset="  
                        + CHARSET + LINE_END);
                sb.append(LINE_END);  
                dos.write(sb.toString().getBytes());  
                InputStream is = new FileInputStream(file);
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = is.read(bytes)) != -1) {
                    dos.write(bytes, 0, len);
                }  
                is.close();  
                dos.write(LINE_END.getBytes());  
                byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)  
                        .getBytes();  
                dos.write(end_data);  
                dos.flush();
                
                int res = conn.getResponseCode();
                if (res == 200) {
                    return SUCCESS;  
                }  
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();  
        } catch (IOException e) {
            e.printStackTrace();  
        }  
        return FAILURE;  
    }
    
    public static void main(String args[]){
    	File file=new File("C:/book3.jpg");
    	try {
			InputStream is = new FileInputStream(file);
			uploadFile(is, file.getName());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    	
    }
}
